/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nts.pointsstore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Himagirinathan
 */
public class Summary extends Activity implements View.OnClickListener
{
  ArrayList<String> checkeditems;
  List<String[]> productslist;
  private DataHelper dh;
  Button bpaybypoints,bpaybycash;
  TextView tvavailablepoints,tvpointsrequired,tvpointscredited;
  long id;
  int availablepoints,pointsrequired,pointscredited;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        checkeditems=this.getIntent().getExtras().getStringArrayList("checked");
        id=this.getIntent().getExtras().getLong("id");
        dh = new DataHelper(this);
        productslist=dh.selectAllProducts();
        availablepoints=dh.selectUserPoints(id);
        for(String arrstr[]:productslist)
        {
          for(int i=0;i<checkeditems.size();i++)
          {
            if(arrstr[0].equals(checkeditems.get(i)))
            {
              pointscredited+=Integer.parseInt(arrstr[2]);
              pointsrequired+=Integer.parseInt(arrstr[3]);
              break;
            }
          }
        }
        pointsrequired*=10;
        this.setContentView(R.layout.summary);
        bpaybycash=(Button)this.findViewById(R.id.paybycash);
        bpaybypoints=(Button)this.findViewById(R.id.paybypoints);
        bpaybycash.setOnClickListener(this);
        bpaybypoints.setOnClickListener(this);
        tvavailablepoints=(TextView)this.findViewById(R.id.availablepoints);
        tvpointsrequired=(TextView)this.findViewById(R.id.pointsrequired);
        tvpointscredited=(TextView)this.findViewById(R.id.pointscredited);
        tvavailablepoints.setText(""+availablepoints);
        tvpointsrequired.setText(""+pointsrequired);
        tvpointscredited.setText(""+pointscredited);
        if(availablepoints<pointsrequired)
         bpaybypoints.setEnabled(false);

    }

    public void onClick(View arg0)
    {
     if(arg0==bpaybycash)
     {
      Intent it=new Intent(this, TxCompleted.class);
      Bundle bundle = new Bundle();
      bundle.putLong("id",id);
      it.putExtras(bundle);
      this.startActivity(it);
     }
     else
     {
      Intent it=new Intent(this, TxCompleted.class);
      Bundle bundle = new Bundle();
      bundle.putLong("id",id);
      it.putExtras(bundle);
      this.startActivity(it);
     }
    }

}
